package com.copm.ifm.servers.sys.controller;

import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.client.SysProjectClient;
import com.copm.ifm.servers.sys.dto.SysProjectDTO;
import com.copm.ifm.servers.sys.entity.SysProjectAuth;
import com.copm.ifm.servers.sys.service.SysProjectService;
import com.copm.ifm.servers.sys.vo.SysProjectAuthVO;
import com.copm.ifm.servers.sys.vo.SysProjectVO;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目表相关接口类
 *
 * @author tp
 * @date 2020/12/15 11:39
 */
@Api(tags = "项目/sys-project")
@RestController
@RequestMapping("/sys-project")
public class SysProjectController extends BaseController implements SysProjectClient {

    @Resource
    SysProjectService service;

    @Override
    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysProjectVO>> page(@RequestBody PageDTO<SysProjectDTO> param) {
        PageVO<SysProjectVO> result = service.selectForPage(param);
        return success(result);
    }

    @ApiOperation("查找-selectById通过id")
    @GetMapping("/by/id")
    public ServiceResponse<SysProjectVO> byId(@RequestParam int id) {
        return success(service.getProjectVoById(id));
    }

    @ApiOperation("修改-update编辑")
    @PutMapping
    public ServiceResponse<?> update(@RequestBody SysProjectDTO entity) {
        service.updateProjectVoById(entity);
        return success(entity);
    }

    @ApiOperation("保存-项目表save")
    @PostMapping
    public ServiceResponse<?> save(@RequestBody SysProjectDTO entity) {
        service.saveSysProjectVO(entity);
        return success(entity);
    }

    /**
     * ------------------------------------------项目授权-------------------------------------------------
     */

    @ApiOperation("项目授权租户分页")
    @PostMapping("/auth/page")
    public ServiceResponse<PageVO<SysProjectAuthVO>> authPage(@RequestBody PageDTO<SysProjectAuth> param) {
        PageVO<SysProjectAuthVO> result = service.selectAuthForPage(param);
        return success(result);
    }

    @ApiOperation("租户对外授权详情")
    @GetMapping("/auth/by/id")
    public ServiceResponse<SysProjectAuthVO> authById(@ApiParam(value = "主键id", required = true)
                                                      @RequestParam int id) {
        return success(service.getAuthTenantById(id));
    }

    @ApiOperation("项目授权租户(单个与批量)")
    @PostMapping("/auth/tenant")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "项目id",name = "projectList",required = true),
            @ApiImplicitParam(value = "授权对象",name = "sysProjectAuth",required = true)})
    public ServiceResponse<?> authTenant(@RequestParam String projectList,
                                         @RequestBody SysProjectAuth sysProjectAuth) {
        List<SysProjectAuth> sysProjectAuthList = service.authTenant(projectList, sysProjectAuth);
        return success(sysProjectAuthList);
    }

    @ApiOperation("update项目授权租户")
    @PutMapping("/auth/tenant")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "主键id",name = "ids"),
            @ApiImplicitParam(value = "项目id",name = "projectIds"),
            @ApiImplicitParam(value = "授权对象",name = "sysProjectAuth",required = true)})
    public ServiceResponse<?> updateAuthTenant(@RequestParam(value = "ids", required = false) String ids,
                                               @RequestParam(value = "projectIds", required = false) String projectIds,
                                               @RequestBody SysProjectAuth sysProjectAuth) {
        List<SysProjectAuth> sysProjectAuthList = service.updateAuthTenant(ids,projectIds,sysProjectAuth);
        return success(sysProjectAuthList);
    }

    @ApiOperation("多项目授权校验")
    @PostMapping("/check/tenant")
    public ServiceResponse<?> checkTenant(@ApiParam(value = "项目id", required = true)
                                          @RequestParam String projectList,
                                          @ApiParam(value = "租户id", required = true)
                                          @RequestParam Integer tenantId) {
        String projectNames = service.checkTenant(projectList,tenantId);
        return success(projectNames);
    }

}
